package com.huilan.hgt.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huilan.hgt.entity.Catalogue;
import com.huilan.hgt.entity.Role;
import com.huilan.hgt.entity.User;
import com.huilan.hgt.service.CatalogueService;
import com.huilan.hgt.service.RoleCatalogueService;
import com.huilan.hgt.service.RoleService;
import com.huilan.hgt.util.PageUtil;
import com.huilan.hgt.util.ResponseResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author ghy
 * @since 2018-11-02
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private CatalogueService catalogueService;

    @Autowired
    private RoleCatalogueService roleCatalogueService;

    /**
     * 跳转角色列表页面
     */
    @GetMapping("/list.do")
    public String page(){
        return "/role/role_list";
    }


    /**
     * 查询角色信息
     */
    @GetMapping("/queryList.do")
    @ResponseBody
    public PageUtil getRoles(PageUtil page){
        roleService.selectRoleList(page);
        return page;
    }


    /**
     * 角色设置
     */
    @GetMapping("/setting.do")
    public String setting(ModelMap modelMap,String roleId){
        //查询所有目录
        List<Catalogue> catalogueList = catalogueService.list(null);
        if(StringUtils.isNotEmpty(roleId)){
            Role role = roleService.getById(roleId);
            //查询该角色已设置的目录
            List<Integer> roleCatalogueList = roleCatalogueService.getCatalogueByRoleId(roleId);
            modelMap.put("role",role);
            modelMap.put("roleCatalogueList",roleCatalogueList);
        }else{
            Role role = new Role();
            modelMap.put("role",role);
        }
        modelMap.put("catalogueList",catalogueList);
        return "role/role_setting";
    }

    /**
     * 修改或保存角色信息
     */
    @PostMapping("/update.do")
    @ResponseBody
    public ResponseResult update(Role role,Integer[] catalogueIds){
        //修改或保存角色信息
        roleService.saveOrUpdateRole(role,catalogueIds);
        return ResponseResult.success();
    }


    /**
     * 删除角色
     * @param role
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/delete.do")
    public ResponseResult delete(Role role) {
        boolean flag = roleService.removeById(role.getRoleId());
        return ResponseResult.success(flag);

    }
}

